#include "GameManager.h"
#include "BaseGame.h"
#include "hgeFont.h"
#include "hgehelper.h"
#include <hgesprite.h>

#include "MenuItem.h"
#include "OverviewScreen.h"
#include "ExploreScreen.h"
#include "IntroScreen.h"
#include "ProduceScreen.h"
#include "BuildScreen.h"
#include "AudioManager.h"
GameManager::GameManager(hgeFont* font)
{
	srand( time_t(0) );
	pFont = font;
	gameData = new GameData();
	gameData->ControlledAcres = 8;
	gameData->HelpersAvailable = 10;
	gameData->HelpersExploring = 0;
	gameData->HelpersWorking = 0;
	gameData->Gold = 900; 
	gameData->GoldCoef = 1.2f;
	gameData->ExploringCoeff =  0.9f;
	gameData->Bartering = 1.0f;
	gameData->CandyCanes = 1;
	gameData->Igloos = 1;
	gameData->CandyCaneFactories = 1;
	gameData->RaindeerFarms = 0;
	gameData->PolarbearGrinders = 0;

	gameData->InhabitansPerIgloo = 10;

	gameData->RaindeerNumOfHelpers = 10;

	for(int i = 0; i < gameData->MaxExpeditions; i++)
	{
		gameData->Expeditions[i] = 0;
		gameData->TimeForExpeditionsToReturn[i] = 0.0f;
	}



	hGui = new hgeGUI();

	HTEXTURE mTex = g_pHge->Texture_Load("Content/cur.png");
	backgroundTexture = g_pHge->Texture_Load("Content/bkg.png");
	backgroundSprite = new hgeSprite(backgroundTexture,0,0,800,600);
	hgeSprite* sprite = new hgeSprite(mTex,0,0,16,16);

	//font->SetScale(0.8f);
	hGui->AddCtrl(new MenuItem( 1,font,0,10,10,0.0f,"Home"));
	hGui->AddCtrl(new MenuItem( 2,font,0,10,35,0.0f,"Explore"));
	hGui->AddCtrl(new MenuItem( 3,font,0,10,60,0.0f,"Build"));
	hGui->AddCtrl(new MenuItem( 4,font,0,10,85,0.0f,"Produce"));
	hGui->AddCtrl(new MenuItem( 5,font,0,10,110,0.0f,"Save"));
	hGui->AddCtrl(new MenuItem( 6,font,0,10,135,0.0f,"Load"));
	hGui->AddCtrl(new MenuItem( 7,font,0,10,160,0.0f,"Exit"));
	hGui->SetCursor(sprite);
	hGui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
	hGui->SetFocus(1);
	hGui->Enter();
	guiId = 0;

	pScreens[0] = new IntroScreen(pFont, gameData);
	pScreens[1] = new OverviewScreen(pFont,gameData);
	pScreens[2] = new ExploreScreen(pFont,gameData);
	pScreens[3] = new BuildScreen(pFont,gameData);
	pScreens[4] = new ProduceScreen(pFont,gameData);

	candycaneTimer = incomeTimer = exploreTimer = m_raindeerUpkeepTimer = m_raindeerBreedTimer = 0;
	exploreTrigger = 10;
	produceTrigger = 10;
	candyTrigger = 5;
	m_raindeerBreedTrigger = 120;
	m_raindeerUpkeepTrigger = 5;
	production = 0.0f;
	m_win = false;
	HTEXTURE winTexture = g_pHge->Texture_Load("Content/win.jpg");
	m_winSprite = new hgeSprite(winTexture,0,0,g_pHge->Texture_GetWidth(winTexture), g_pHge->Texture_GetHeight(winTexture));

	new AudioManager();
	g_pAudioManager->Initialize();
	g_pAudioManager->PlaySong("xmas", true);
}

GameManager::~GameManager()
{
	
}

bool GameManager::Render()
{
	/**************** Rendering start Here *******************/
	if(!m_win)
	{
		pFont->SetScale(0.7);
		pScreens[guiId]->Render();
		backgroundSprite->Render(0,0);

		hGui->Render();
	}
	else
		m_winSprite->RenderStretch(0,0,g_pHge->System_GetState(HGE_SCREENWIDTH), g_pHge->System_GetState(HGE_SCREENHEIGHT));

	/**************** Rendering End Here *******************/

	return true;
}

bool GameManager::Update(float fDeltaTime)
{
	/*************** Gamelogic Start Here ******************/
	
	result = UpdateGUI(fDeltaTime);
	
	pScreens[guiId]->Update(fDeltaTime);
	
	UpdateInput(fDeltaTime);

	UpdateTimers(fDeltaTime);
	
	CheckTriggers(fDeltaTime);

	UpdateGameDataMembers();

	if(gameData->Raindeers >= 8)
	{
		m_win = true;
	}

	/*************** Gamelogic End Here *******************/
	return result;
}

void GameManager::UpdateGameDataMembers()
{
	gameData->CandyCaneUpkeep = max(1,(gameData->HelpersExploring + gameData->HelpersWorking + gameData->HelpersAvailable) * 0.1 * (1 + (gameData->Raindeers * 0.1)));

}

bool GameManager::UpdateGUI(float fDeltaTime)
{
	int id = hGui->Update(fDeltaTime);
	if(id != 0)
	{
		switch (id)
		{
		case 1:
		case 2:
		case 3:
		case 4:
			guiId = id;
		break;
		case 5:
			Save();
			break;
		case 6:
			Load();
			break;
		case 7:
		return false;
			break;
		default:
			break;
		}
	}
	return true;
}
void GameManager::UpdateInput(float fDeltaTime)
{
	
	/*switch(g_pHge->Input_GetKey())
	{
	default:
		break;
	}*/
	
}
void GameManager::UpdateTimers(float fDeltaTime)
{

	for(int i = 0; i < gameData->MaxExpeditions; i++)
	{
		if(gameData->Expeditions[i] > 0)
			gameData->TimeForExpeditionsToReturn[i] -= fDeltaTime;
	}

	if(gameData->RaindeerFarms > 0) 
		m_raindeerUpkeepTimer += fDeltaTime;

	candycaneTimer += fDeltaTime;

	if(gameData->HelpersWorking > 0)
	{
		production += fDeltaTime * gameData->HelpersWorking;
		incomeTimer += fDeltaTime;
	}

	if(gameData->RaindeerFarms > 0)
		m_raindeerBreedTimer += fDeltaTime;

}
void GameManager::CheckTriggers(float fDeltaTime)
{
	
	if(incomeTimer >= produceTrigger)
	{
		gameData->Gold += gameData->GoldCoef * production;
		incomeTimer -= incomeTimer;
		production = 0;
		g_pAudioManager->PlayEffect("cash");
	}

	for(int i = 0; i < gameData->MaxExpeditions; i++)
	{
		if(gameData->TimeForExpeditionsToReturn[i] < 0)
		{
			gameData->TimeForExpeditionsToReturn[i] = 0.0f;

			gameData->ControlledAcres += gameData->ExploringCoeff * gameData->Expeditions[i] * gameData->TimeInExpedition[i];
			gameData->HelpersAvailable += gameData->Expeditions[i];
			gameData->HelpersExploring -= gameData->Expeditions[i];
			gameData->Expeditions[i] = 0;
			g_pAudioManager->PlayEffect("yay");
		}

	}



	if(candycaneTimer >= candyTrigger)
	{
		candycaneTimer -= candyTrigger;
		
		gameData->CandyCanes -= gameData->CandyCaneUpkeep;
		gameData->CandyCanes += gameData->CandyCaneFactories * 2;

		gameData->CandyCanes = max(0, gameData->CandyCanes);

		if(gameData->CandyCanes == 0)
		{
			rand() % 1 ? g_pAudioManager->PlayEffect("die1") : g_pAudioManager->PlayEffect("die2");

			if(gameData->HelpersExploring > 0)
			{
				for(int i = 0; i < gameData->MaxExpeditions; i++)
				{
					gameData->Expeditions[i] -= rand() % 2+1;
				}
			}
			else if(gameData->HelpersWorking > 0)
			{
				gameData->HelpersWorking -= rand() % 2+1;
			}
			else
				gameData->HelpersAvailable -= rand() % 2+1 * gameData->Igloos;
		}
		else
		{
			if(gameData->HelpersExploring + gameData->HelpersWorking + gameData->HelpersAvailable < gameData->Igloos * gameData->InhabitansPerIgloo)
			{
				gameData->HelpersAvailable++;
			}
		}
	}


	if(gameData->RaindeerFarms > 0 && m_raindeerUpkeepTimer >= m_raindeerUpkeepTrigger)
	{
		m_raindeerUpkeepTimer -= m_raindeerUpkeepTrigger;

		if(gameData->HelpersAvailable > gameData->RaindeerNumOfHelpers * gameData->Raindeers)
			gameData->HelpersAvailable -= gameData->RaindeerNumOfHelpers * gameData->Raindeers;
		else
		{
			gameData->HelpersAvailable = 0;
			gameData->Raindeers--;
			g_pAudioManager->PlayEffect("rainDie");
		}
	}

	if(gameData->RaindeerFarms > 0 && m_raindeerBreedTimer >= m_raindeerBreedTrigger)
	{
		gameData->Raindeers++;
		m_raindeerBreedTimer -= m_raindeerBreedTrigger;
	}

}

bool GameManager::RestoreFunc()
{

  return false;
}

void GameManager::Save()
{
	std::ofstream ofs("saved.txt");

	ofs << *gameData; // store the object to file
	ofs.close();

}

void GameManager::Load()
{
	GameData data;

	std::ifstream file("saved.txt");
	file >> data;
	file.close();

	memcpy ( gameData, &data, sizeof(GameData) );

}